package obecto.navigation
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	[DefaultProperty("navigatorReferences")]
	
	[Event(name="currentSectionPathChanged", type="flash.events.Event")]
	
	public class Navigator extends EventDispatcher
	{		
		private var _navigatedSection : String
		[Bindable("navigatedSectionChanged")]
		public function get navigatedSection() : String
		{
			return _navigatedSection;	
		}
		
		private var _currentSection : String;
		[Bindable]
		public function set currentSection(value : String) : void
		{
			_currentSection = value;
			updateCurrentSectionPath();
			dispatchEvent(new Event("currentSectionPathChanged"));
		}
		public function get currentSection() : String
		{
			return _currentSection;
		}
		
		private var _currentSectionPath : String;
		[Bindable("currentSectionPathChanged")]
		public function get currentSectionPath() : String
		{
			return _currentSectionPath;
		}
		
		
		private var _sectionPath : String;
		public function set sectionPath(value : String) : void
		{
			if (_sectionPath != value)
			{
				_sectionPath = value;
				processSectionPath();
			}
		}
		
		[ArrayElementType("obecto.navigation.NavigatorReference")]
		private var _navigatorReferences : Array;
		public function set navigatorReferences(value : Array) : void
		{
			var item : NavigatorReference;
			if (_navigatorReferences && _navigatorReferences.length > 0)
			{
				for each (item in _navigatorReferences)
				{
					item.navigator.removeEventListener("currentSectionPathChanged", handleNavigatorReferenceCurrentSectionChanged);
				}
			}
			
			_navigatorReferences = value;
			
			if (_navigatorReferences && _navigatorReferences.length > 0)
			{
				for each (item in _navigatorReferences)
				{
					if (item.navigator)
					{
						item.navigator.addEventListener("currentSectionPathChanged", handleNavigatorReferenceCurrentSectionChanged);
					}
				}
			}
		}
		
		public function Navigator()	
		{	
		}
		
		private function processSectionPath() : void
		{
			if (_sectionPath && _sectionPath.length > 0)
			{
				var arr : Array = _sectionPath.split('/', 2);
				
				if (arr.length > 1)
				{
					_navigatedSection = arr[0];
					var navigatorReference : NavigatorReference = getNavigatorReference(arr[0]);
					if (navigatorReference && navigatorReference.navigator)
					{
						navigatorReference.navigator.sectionPath = _sectionPath.replace(arr[0] + "/", "");
					}
				}
				else
				{
					_navigatedSection = _sectionPath;
				}
				
				dispatchEvent(new Event("navigatedSectionChanged"));
				_currentSection = _navigatedSection;
			}
		}
		
		private function getNavigatorReference(name : String) : NavigatorReference
		{
			for each (var navigatorReference : NavigatorReference in _navigatorReferences)
			{
				if (navigatorReference.name == name)
				{
					return navigatorReference;
				}
			}
			
			return null;
		}
		
		private function handleNavigatorReferenceCurrentSectionChanged(event : Event) : void
		{
			if (_currentSection)
			{
				_currentSectionPath = _currentSection + "/" + Navigator(event.target).currentSectionPath;
			}
			else
			{
				_currentSectionPath = Navigator(event.target).currentSectionPath;
			}
			dispatchEvent(new Event("currentSectionPathChanged"));
		}
		
		private function updateCurrentSectionPath() : void
		{
			var navigatorReference : NavigatorReference = getNavigatorReference(_currentSection);
			
			if (navigatorReference && navigatorReference.navigator)
			{
				_currentSectionPath = _currentSection + 
					navigatorReference.navigator._currentSectionPath;
			}
			else
			{
				_currentSectionPath = _currentSection;
			}
		}
	}
}